package yxy.game.pm2.command.pack;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.debris;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.Collection;

/** 背包 碎片合成 */
@MessageType(proto = Command.ChipCompound, classtype = HeroMessage.ChipCompoundRequest.class)
public class CMD_ChipCompound extends Command<HeroMessage.ChipCompoundRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.ChipCompoundRequest request) {
		String playerCode = messageSource.getTag();
		int dataId = request.getDataId();
		int num = request.getNum();

		HeroMessage.ChipCompoundResponse.Builder response = HeroMessage.ChipCompoundResponse.newBuilder();

		debris data = DataCenter.getData(dataId, debris.class);
		if (data == null || num < 1) {
			send(messageSource, response, Result.ParamError);
			return;
		}

		HeroHandle herosModule = new HeroHandle();
		int upper = herosModule.getUpper(playerCode);
		if (herosModule.getHerosSize(playerCode) + num > upper) {
			// 武将数超上限
			send(messageSource, response, Result.HeroCountUpper);
			return;
		}
		
//		Currencies rewards = new HeroHandle().chipCompoundRewards(data, num);
//		Currency cost = Currency.create(CurrencyType.CT_碎片, dataId, data.getSynthesis() * num);
//		Result result = new PackageHandle().exchange(playerCode, new Currencies(cost), rewards, CCReason.碎片合成);
//		if (!result.equals(Result.Success)) {
//			send(messageSource, response, result);
//			break;
//		}
		

		Currency cost = Currency.create(CurrencyType.CT_碎片, dataId, data.getSynthesis() * num);
		try {
			new CurrencyHandle().cost(playerCode, CCReason.碎片合成, cost);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		}

		try {
			Collection<Hero> heros = new HeroHandle().chipCompound(playerCode, data, num);
			for (Hero hero : heros) {
				response.addHeros(new HeroHandle().getHeroSimple(playerCode, hero));
			}
			response.setItem(new CurrencyHandle().packItem(Item.get(playerCode, CurrencyType.CT_碎片, dataId)));
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);
			return;
		}
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, HeroMessage.ChipCompoundResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}